const fs = require('fs');
const path = require('path');
var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var controller = require(tools.rootPath + 'libs/controller.js');

const jwt = require("jsonwebtoken");

module.exports = class extends controller {
    constructor() {
        super();
    }
    //生成token
    index(req, res) {
        var profile = {
            name: 'david',
            email: 'david@alibaba.com',
            id: 123,
        };

        // 根据profile信息生产token
        var token = jwt.sign(profile, 'szh2020', { expiresIn: 60 * 60 });//1小时
        res.end(token);
    }
    check(req, res) {
        var token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiZGF2aWQiLCJlbWFpbCI6ImRhdmlkQGFsaWJhYmEuY29tIiwiaWQiOjEyMywiaWF0IjoxNTkxMzQ3MjM0LCJleHAiOjE1OTEzNDcyNjR9.laSIVqGaAim0LUKuv5YEP-Xni-eD_-8dsU1yKH0HX2I';

        try {
            var decoded = jwt.verify(token, 'szh2020');
            res.end(JSON.stringify(decoded));
        } catch (err) {
            console.log(err);
            res.end(JSON.stringify(err));
        }
        // jwt.verify(token, 'szh2020', function (err, decoded) {
        //     console.log(err, decoded);
        //     if (err) {
        //         res.end('invalid');
        //         return;
        //     }
        //     res.end(JSON.stringify(decoded));
        // });
    }
}